package org.systm.adminuser.entity;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import net.minidev.json.annotate.JsonIgnore;
import org.global.GEntity;
import org.springframework.security.core.GrantedAuthority;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Transient;
import java.util.Objects;

@Getter
@Setter
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
@Entity(name = "sys_user_per")
public class UserPermission extends GEntity<String> implements GrantedAuthority {
    @Column(columnDefinition = "varchar(50) not null comment '权限标识'")
    private String per;

    @Column(columnDefinition = "varchar(255) not null comment '请求url'")
    private String url;

    @Column(columnDefinition = "varchar(20) not null comment '请求方法'")
    private String method;

    @Column(columnDefinition = "varchar(255) not null comment '描述'")
    private String spec;

    @Override
    @Transient
    @JsonIgnore
    public String getAuthority() {
        return this.per;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        UserPermission userPermission = (UserPermission) obj;
        return Objects.equals(userPermission.getPer(), getPer());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getPer());
    }
}
